#ifndef WINDOW_MANAGER_H
#define WINDOW_MANAGER_H

#include <windows.h>
#include <d2d1_1.h>
#include <d3d11.h>
#include <dxgi1_2.h>
#include <dcomp.h>
#include <wincodec.h>
#include <shellapi.h>
#include <string>
#include <functional>
#include <wrl/client.h>

using Microsoft::WRL::ComPtr;

// Forward declaration
class DataManager;

class WindowManager {
public:
    WindowManager();
    ~WindowManager();

    bool Initialize();
    void CreateMainWindow();
    void ShowWindow();
    void UpdateClickCount(int count);
    void SetNekoState(bool isActive);
    void EnsureAlwaysOnTop(); // Ensure window stays on top
    HWND GetWindowHandle() const { return m_hwnd; }
    
    // Data management
    DataManager* GetDataManager() { return m_dataManager; }
    
    // System tray functionality
    bool CreateSystemTray();
    void RemoveSystemTray();
    void ResetClickCount();
    
    // Callback function type for reset count events
    using ResetCallback = std::function<void()>;
    void SetResetCallback(ResetCallback callback) { m_resetCallback = callback; }
    
    static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

private:
    HWND m_hwnd;
    HINSTANCE m_hInstance;
    
    // DirectComposition resources
    ComPtr<IDCompositionDevice> m_dcompDevice;
    ComPtr<IDCompositionTarget> m_dcompTarget;
    ComPtr<IDCompositionVisual> m_dcompVisual;
    
    // Direct2D resources
    ComPtr<ID3D11Device> m_d3dDevice;
    ComPtr<IDXGIDevice> m_dxgiDevice;
    ComPtr<IDXGIFactory2> m_dxgiFactory;
    ComPtr<IDXGISwapChain1> m_swapChain;
    ComPtr<ID2D1Factory1> m_d2dFactory;
    ComPtr<ID2D1Device> m_d2dDevice;
    ComPtr<ID2D1DeviceContext> m_d2dContext;
    ComPtr<IWICImagingFactory> m_wicFactory;
    
    // Images
    ComPtr<ID2D1Bitmap> m_nekoDownImage;
    ComPtr<ID2D1Bitmap> m_nekoUpImage;
    bool m_isNekoActive;
    
    // Data management
    DataManager* m_dataManager;
    
    // System tray
    NOTIFYICONDATA m_notifyIconData;
    bool m_trayIconCreated;
    ResetCallback m_resetCallback;
    
    // Window properties
    static const int WINDOW_WIDTH = 80;
    static const int WINDOW_HEIGHT = 80;
    
    // Timer for neko animation
    static const UINT_PTR NEKO_TIMER_ID = 1;
    static const UINT NEKO_ANIMATION_DURATION = 150; // 0.15 seconds in milliseconds
    
    // Drag functionality
    bool m_isDragging;
    int m_dragOffsetX;
    int m_dragOffsetY;
    
    // GDI fallback image support
    HBITMAP m_hBitmapNekoDown;
    HBITMAP m_hBitmapNekoUp;
    
    // Private methods
    bool InitializeDirectComposition();
    bool InitializeDirect2DFallback();
    void CleanupDirectComposition();
    bool CreateSwapChain();
    bool LoadImagesFromResource();
    void DrawFrame();
    bool DrawDirect2D(HDC hdc);
    void DrawGDIFallback(HDC hdc);
    void DrawCountOverlay(HDC hdc);
    HBITMAP LoadPNGResourceAsHBITMAP(int resourceId);
    void LoadGDIImages();
    void CleanupGDIImages();
    ComPtr<ID2D1Bitmap> LoadPNGFromResource(int resourceId);
    void LoadPNGsForFallback(ID2D1DCRenderTarget* renderTarget);
    ComPtr<ID2D1Bitmap> LoadPNGBitmapForFallback(int resourceId, ID2D1DCRenderTarget* renderTarget);
    HICON CreateIconFromPNGResource(int resourceId);
    void ShowTrayContextMenu(int x, int y);
    void HandleTrayIcon(WPARAM wParam, LPARAM lParam);
    void CalculateBottomRightPosition(int& x, int& y);
    void OnNekoAnimationTimer(); // Handle neko animation timer
    void ShowAboutDialog(); // Show about dialog
    LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    // Rendering mode
    bool m_useDirectComposition;
};

#endif // WINDOW_MANAGER_H
